import express from 'express';
import {Space} from '../db/model.js';

const router = express.Router();
router.post('/add', async (req, res) => {
    try {
        if (!req.body) {
            throw Error('没有数据')
        }

        const data = await Space.create(req.body)
        res.status(200).send({msg: '添加成功'})
    } catch (err) {
        res.status(500).send({msg: err.message})
    }
})

router.put('/update', async (req, res) => {
    try {
        if (!req.body) {
            throw Error('没有数据')
        }
        if (!req.body._id) {
            throw Error('缺少id')
        }
        const data = await Space.updateOne({_id: req._id}, req.body)
        res.status(200).send({msg: '更新成功'})
    } catch (err) {
        res.status(500).send({msg: err.message})
    }
})

router.delete('/del', async (req, res) => {
    try {
        const {_id} = req.query

        if (!_id) {
            throw Error('缺少id')
        }

        const data = await Space.deleteOne({_id})
        res.status(200).send({msg: '删除成功'})
    } catch (err) {
        res.status(500).send({msg: err.message})
    }
})

export default router;
